package be.vanbaelvandaele.tuxremote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

	private ImageButton previousButton;
	private ImageButton nextButton;
	private ImageButton playPauseButton;
	private SeekBar volumeBar;

	private MediaPlayerInterface player;

	public void display(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		previousButton = (ImageButton) findViewById(R.id.previousButton);
		previousButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String result = player.previous();
			}
		});

		playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);
		playPauseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String result = player.playPause();
			}
		});

		nextButton = (ImageButton) findViewById(R.id.nextButton);
		nextButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String result = player.next();
			}
		});

		volumeBar = (SeekBar) findViewById(R.id.volumeBar);
		volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			private int progress;

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				String result = player.setVolume(progress);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				this.progress = progress;
			}
		});

		Spinner s = (Spinner) findViewById(R.id.spinnerMediaInterface);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.mediainterfaces, android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);

		s.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String item = (String) arg0.getItemAtPosition(arg2);

				if (item.equalsIgnoreCase("rhythmbox")) {
					player = new RhythmboxMediaPlayer(MainActivity.this);
				} else if (item.equalsIgnoreCase("vlc")) {
					player = new VlcMediaPlayer(MainActivity.this);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.quitMenuItem:
			display("Bye bye!");
			finish();
			break;
		case R.id.settingsMenuItem:
			showSettings();
			break;
		case R.id.filebrowserMenuItem:
			showFileBrowser();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void showFileBrowser() {
		startActivity(new Intent(this.getApplicationContext(),
				FileBrowser.class));
	}

	private void showSettings() {
		startActivityForResult(new Intent(this.getApplicationContext(),
				Settings.class), 1);
	}

}